package com.ucar.train.service.impl;

import com.github.pagehelper.PageHelper;
import com.ucar.train.dao.MenuMapper;
import com.ucar.train.dao.RoleAndMenuMapper;
import com.ucar.train.dto.MenuDTO;
import com.ucar.train.service.MenuService;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.utils.MailUtils;
import com.ucar.train.vo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujinmu
 * @title MenuServiceImpl
 * @projectName train-parent
 * @description TODO
 * @created 2019-07-24 15:27
 **/
@Service
public class MenuServiceImpl implements MenuService {
  @Autowired
  private MenuMapper menuMapper;

  @Autowired
  private RoleAndMenuMapper roleAndMenuMapper;

  @Override
  public void deleteMenu(Long id) {

    menuMapper.deleteMenu(id);
    roleAndMenuMapper.deleteMenuToRole(id);
  }

  @Override
  public void addMenu(Menu menu) {
    List<Menu> menus = new ArrayList<>();
    menu.setCreateTime(JodaUtils.nowAsDate());
    menu.setModifyTime(JodaUtils.nowAsDate());
    menus.add(menu);
    menuMapper.insertMenus(menus);
  }

  @Override
  public void updateMenu(Menu menu) {
    menu.setModifyTime(JodaUtils.nowAsDate());
    menuMapper.updateMenu(menu);
  }

  @Override
  public List<MenuDTO> getMenu(Integer page,Integer size,String orderBy) {
    PageHelper.startPage(page, size,orderBy);
    return menuMapper.getAllMenu();
  }

  @Override
  public List<MenuDTO> listMenus() {
    List<MenuDTO> menus = menuMapper.getAllMenu();
    return menus;
  }

  @Override
  public MenuDTO getMenuById(Long id) {
    MenuDTO menu = menuMapper.getMenuById(id);
    return menu;
  }
}
